﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:url value="/car/info" var="userInfoUrl"/>
<c:url value="/car/delete" var="userDeleteUrl"/>

<script type="text/javascript">

function carInfo(carId) {
	
	var json = { carId: carId };
	$.ajax({
        url: "${userInfoUrl}",
        data: JSON.stringify(json),
        type: "POST",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
        },
        success: function(obj) {
        	$('#result').html("");
    		//var obj = JSON.parse(response);
    		$('#result').html("Car | Автомобиль: " + obj.carBrand +" "+ obj.color +"</br>State Number | Гос.номер: " + obj.stateNumber);
        },
        error: function(){
            alert('failure');
        }
    });
}

function carDelete(carId) {
	
	var json = { id: carId };
	$.ajax({
        url: "${userDeleteUrl}/" + carId + "/ajax",
        type: "POST",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
        },
        success: function(obj) {
        	if (obj) {
        		var tr = $("#c"+carId);
                tr.css("background-color","#FF3700");

                tr.fadeOut(400, function(){
                    tr.remove();
                });
                
        		$('#result').html("Deleted car: " + obj.carBrand + "  " + obj.color + " " + obj.stateNumber);
        	} else {
        		$('#result').html("Error");
        	}
    		
        },
        error: function(){
            alert('failure');
        }
    });
}
</script>

<h1>Список пользователей: </h1>
 
<a href="<c:url value="/user/new" />">Добавить в список</a> 

<br/><br/>
<table id="carsTable" border="1" cellpadding="10" cellspacing="0" class="table table-striped table-bordered">
	<tr>
		<th>ID</th>
		<th>Логин</th>
		<th>Пароль</th>
		<th>ФИО</th>
		<th>Е-мэйл</th>
                <th>№ паспорта</th>
                <th>Права доступа</th>
		<th></th>
	</tr>
	
	<c:forEach items="${users}" var="user">
		<c:url value="/user/${user.userId}" var="viewUserUrl"/>
		
		<tr id="c${user.userId}">
		    <td>${user.userId}</td>
		    <td><a href="${viewUserUrl}">${user.login}</a></td>
		    <td>${user.password}</td> 
		    <td><a href="${viewUserUrl}">${user.name}</a></td>
		    <td>${user.email}</td> 
		    <td>${user.passportCode}</td> 
		    <td>${user.accessRole}  &nbsp;  &nbsp;</td>
		    
		    
		    <td>
		    	<a href="<c:url value="/user/edit/${user.userId}" />"> Edit </a> &nbsp;
		    	<a href="<c:url value="/user/delete/${user.userId}" />"> Delete </a>
		   	</td>
	   	</tr>
	</c:forEach>
</table>

<div id="result"></div>
 
